package com.example.homework;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentContainerView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.NavHostController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.homework.ui.BlockedFragment;
import com.example.homework.ui.ContactsFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private FragmentManager fragmentManager;
    private BottomNavigationView bottomNavigationView;
    private ContactsFragment contactsFragment;
    private BlockedFragment blockedFragment;
    private FragmentContainerView fragmentContainerView;
    private NavController navController;
    private NavHostFragment navHostFragment;
    public DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        init();
    }
    private void init(){
        fragmentManager=getSupportFragmentManager();
        bottomNavigationView=findViewById(R.id.bottom_nav);
        contactsFragment=new ContactsFragment();
        blockedFragment=new BlockedFragment();
        fragmentContainerView=findViewById(R.id.fragment_view);
        navHostFragment=(NavHostFragment) fragmentManager.findFragmentById(R.id.fragment_view);
        navController=navHostFragment.getNavController();
        this.dbHelper=new DBHelper(this);

        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_contact, R.id.nav_blocked)
                .build();

        NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
        NavigationUI.setupWithNavController(bottomNavigationView,navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId()==R.id.menu_logout){
            Intent intent=new Intent();
            intent.setClass(this, LoginActivity.class);
            startActivity(intent);
            return true;
        }
        if (item.getItemId()==R.id.menu_source){
            Intent intent=new Intent();
            intent.setClass(this, WebViewActivity.class);
            intent.putExtra("url","https://gitee.com/song-of-the-moon/android-homework");
            startActivity(intent);
            return true;
        }
        return false;
    }
}